﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MipsPipelineSimulator
{
    public partial class StringControl : UserControl
    {
        private Font font;

        private Brush brush;

        private Direction textDirection;

        private String showText;

        private int rotateAngle = 0; 
  
        private Point origin = new Point(0,0); 

        private  Size lastSize = new Size(0,0);
 
        private StringControl()
        {
            
        }
        public StringControl(String showText, Font font, Brush brush, Direction textDirection)
        {
            this.showText = showText;
            this.font = font;
            this.brush = brush;
            this.textDirection = textDirection;
            InitializeComponent();
        }

         public void Compute() 
         {
             SizeF sizeF = ComputeSize();
             Size size = sizeF.ToSize();
             switch(this.textDirection) 
             { 
                 case Direction.LeftToRight: 
                     { 
                         lastSize = new Size(size.Width, size.Height); ;
                         this.rotateAngle = 0; 
                         this.origin = new Point(0, 0);
                         this.Size = lastSize;
                         break; 
                     }
                 case Direction.TopToBottom: 
                     { 
                         lastSize = new Size(size.Height, size.Width); 
                         this.rotateAngle = 90; 
                         this.origin = new Point(lastSize.Width, 0);
                         this.Size = lastSize;
                         break; 
                     }
                 case Direction.BottomToTop: 
                     { 
                         lastSize = new Size(size.Height, size.Width); 
                         this.rotateAngle = 270; 
                         this.origin = new Point(0, lastSize.Height);
                         this.Size = lastSize;
                         break; 
                     }
                 case Direction.RightToLeft: 
                     {
                         lastSize = new Size(size.Width, size.Height); 
                        this.rotateAngle = 180; 
                        this.origin = new Point(lastSize.Width, lastSize.Height);
                        this.Size = lastSize;
                        break; 
                     } 
             } 
         }

         public SizeF ComputeSize()
         {
             Graphics g = this.CreateGraphics();
             SizeF size = g.MeasureString(showText, font);
             return size;
         }  

        private void StringControl_Paint(object sender, PaintEventArgs e)
        {
            Size size = this.Size;
            Graphics g = e.Graphics;
            if (lastSize != size)
            {
                this.Compute();
            }
            g.TranslateTransform(this.origin.X, this.origin.Y);
            g.RotateTransform(this.rotateAngle);
            g.DrawString(this.showText, this.font, this.brush,0,0);
            g.ResetTransform();
        }

        public Direction GetTextDirction()
        {
            return textDirection;
        }
    }
}
